using System;
using System.Threading;
using Comdiv.QueryEngine;

namespace Comdiv.QueryEngine.Tests{
    public class testQueryStep : IQueryProcessorStep<testQuery,int>{
        public string Name { get; set; }
        public bool MakeError { get; set; }
        public bool MakeResult { get; set;}
        public bool MakeFinish { get; set;}
        public int Result { get; set; }
        public bool MakeRandomPause { get; set; }
        public Action<testQuery> Custom { get; set; }
        public void Process(testQuery query){
            if(MakeRandomPause){
                Thread.Sleep(new Random().Next(50,200));
            }
            if(MakeFinish){
                query.IsFinished = true;
            }
            if(MakeResult||Result!=0){
                query.Result = Result;
            }
            if(Custom!=null){
                Custom(query);
            }
            if(MakeError){
                if(SlightError){
                    query.Error = new Exception(Name);
                }
                else{
                    throw new Exception(Name);    
                }
                
            }
        }

        public int Idx
        {
            get; set;

        }

        public bool SlightError { get; set; }
    }
}